﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

using Fly.Common;
using Fly.Common.Linq;
using Fly.FlyCommon;
using Fly.Log;
using OA.DAL;
using OA.Entity;

namespace OA.BLL
{
    public class MaterialBLL
    {
        #region dbcontext

        private MaterialDBContext dbContext
        {
            get;
            set;
        }

        #endregion

        #region 构造方法

        public MaterialBLL()
        {
            this.dbContext = new MaterialDBContext();
        }

        #endregion

        #region 属性



        #endregion

        #region 公有方法

        /// <summary>
        /// 根据条件获取物资集合。
        /// </summary>
        /// <param name="condition">查询条件。</param>
        /// <param name="pageCount">页数。</param>
        /// <param name="pageSize">页面大小。</param>
        /// <returns></returns>
        public List<Material> GetMaterialList(QueryMaterialCondition condition, int pageCount, int pageSize)
        {
            var query = from m in dbContext.DbMaterial
                        select m;
            query = query.BuildCondition<Material>(condition);
            if (pageSize > 0)
            {
                query = query.OrderBy(q => q.id).Skip(pageCount * pageSize).Take(pageSize);
            }
            return query.ToList();
        }

        /// <summary>
        /// 获取物资单体。
        /// </summary>
        /// <param name="id">主键。</param>
        /// <param name="status">状态。</param>
        /// <returns></returns>
        public Material GetMaterialInfo(int id, int status)
        {
            return dbContext.DbMaterial.SingleOrDefault(m => m.id == id && m.iStatus == status);
        }

        /// <summary>
        /// 添加物资。
        /// </summary>
        /// <param name="materialInfo"></param>
        /// <returns></returns>
        public Result AddMaterialInfo(Material materialInfo, UserInfo userInfo)
        {
            Result result = new Result()
            {
                IsSucceed = false
            };
            try
            {
                materialInfo = dbContext.DbMaterial.Add(materialInfo);
                dbContext.SaveChanges();
                result.IsSucceed = true;
                result.Message = "物资添加成功！";
                UserLogManager.Log(new User_Log()
                {
                    //FunctionId="物料模块",
                    //FunctionType="添加物料",
                    Content = "物资添加成功",
                    IsrtUserName = userInfo.UserName,
                    IsrtUser = userInfo.UserId,
                    IsrtUserDept = userInfo.UserDept,
                    IsrtUserRole = userInfo.UserRole,
                    ObjectId = materialInfo.id.ToString()
                });
            }
            catch (Exception ex)
            {
                result.Message = "物资添加失败：" + ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 修改物资。
        /// </summary>
        /// <param name="materialInfo"></param>
        /// <returns></returns>
        public Result EditMaterialInfo(Material materialInfo, UserInfo userInfo)
        {
            Result result = new Result()
            {
                IsSucceed = false
            };
            var material = dbContext.DbMaterial.SingleOrDefault(m => m.id == materialInfo.id);
            if (material == null)
            {
                result.Message = "物资修改失败：未找到此物资信息！";
                return result;
            }
            int id = material.id;
            try
            {
                EntityHelper.ConvertEntity2Entity<Material>(materialInfo, material);
                material.id = id;
                dbContext.SaveChanges();
                result.IsSucceed = true;
                result.Message = "物资修改成功！";
                UserLogManager.Log(new User_Log()
                {
                    Content = "物资修改成功",
                    IsrtUserName = userInfo.UserName,
                    IsrtUser = userInfo.UserId,
                    IsrtUserDept = userInfo.UserDept,
                    IsrtUserRole = userInfo.UserRole,
                    ObjectId = materialInfo.id.ToString()
                });
            }
            catch (Exception ex)
            {
                result.Message = "物资修改失败：" + ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 删除物资。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result DelMaterialInfo(int id, UserInfo userInfo)
        {
            Result result = new Result()
            {
                IsSucceed = false
            };
            var material = dbContext.DbMaterial.SingleOrDefault(m => m.id == id);
            if (material == null)
            {
                result.Message = "物资删除失败：未找到此物资信息！";
                return result;
            }
            material.iStatus = 1;
            try
            {
                dbContext.SaveChanges();
                result.IsSucceed = true;
                result.Message = "物资删除成功！";
                UserLogManager.Log(new User_Log()
                {
                    Content = "物资删除成功",
                    IsrtUserName = userInfo.UserName,
                    IsrtUser = userInfo.UserId,
                    IsrtUserDept = userInfo.UserDept,
                    IsrtUserRole = userInfo.UserRole,
                    ObjectId = id.ToString()
                });
            }
            catch (Exception ex)
            {
                result.Message = "物资删除失败：" + ex.Message;
            }
            return result;
        }

        #endregion
    }
}
